package com.yjzx.util.service.pay.listener;

import com.yjzx.util.service.pay.enums.ClientEnum;
import com.yjzx.util.service.pay.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author yjzx
 * @Description 监听支付成功通知
 * @create 2022-12-29 13:49
 */
@Slf4j
@RestController
public class PayListener {


    @Autowired
    PayService payService;
    @GetMapping("/pay/{payType}/{client}")
    public String payChargebacks(@RequestParam("orderId") String orderId,
                                 @PathVariable("payType") String payType,
                                 @PathVariable("client") String client){
        ClientEnum clientEnum = ClientEnum.valueOf(client.toUpperCase());

        Boolean b = payService.payChargebacks(orderId,payType,clientEnum);
        return b?"ok":"fail";
    }
    @GetMapping("/pay/query/{payType}/{client}")
    public String payQuery(@RequestParam("orderId") String orderId,
                                 @PathVariable("payType") String payType,
                                 @PathVariable("client") String client){
        ClientEnum clientEnum = ClientEnum.valueOf(client.toUpperCase());

        return payService.payQuery(orderId,payType,clientEnum);
    }
    @RequestMapping("/pay/notify/{payType}/{client}")
    public Object payStatusCallback(HttpServletRequest request,
                                    @RequestParam Map<String,String> params,
                                    @RequestHeader Map<String,String> headers,
                                    @RequestBody String body,
                                    @PathVariable("payType") String payType,
                                    @PathVariable("client") String client){
        ClientEnum clientEnum = ClientEnum.valueOf(client.toUpperCase());

        Object notify = payService.processNotify(request,params,headers,body,payType,clientEnum);


        return notify;
    }

}
